// pages/home/bargain/detail/detail.js
const app = getApp();
var util = require('../../../../utils/util')
var { _get_activity_detail, _apply_bargain_record, _apply_bargain_info, _bargain } = require('../../../../utils/xiaohuangyu')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: '',
    bargain_apply_id: '',
    activityDetail: {},
    userInfo: {},
    apply_bargain_info: {},
    bargain_record_list: [],
    modalName: '',
    bargain_amount: 0,
    end_time: '2020-12-04 10:05:10',
    countdown_time: {},
    countdown: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityId: options['id'],
      bargain_apply_id: options['bargain_apply_id'],
      userInfo: app.globalData.userInfo,
    })
    // 活动信息
    _get_activity_detail(this.data.activityId, res => {
      console.log('活动信息', res)
      if (res.status) {
        // 砍价记录列表
        this.get_apply_bargain_record()
        // 报名信息
        this.get_apply_bargain_info()
        this.setData({
          activityDetail: res.data
        })
      }
    })
  },
  get_apply_bargain_record() {
    // 砍价记录列表
    _apply_bargain_record(this.data.activityId, { 'apply_id': this.data.bargain_apply_id }, reqs => {
      console.log('砍价记录列表', reqs)
      this.setData({
        bargain_record_list: reqs.data.dataList
      })
    })
  },
  get_apply_bargain_info() {
    let that = this
    // 报名信息
    _apply_bargain_info(that.data.activityId, that.data.bargain_apply_id, reps => {
      console.log('报名信息', reps)
      if (reps.status) {
        // 计算倒计时
        that.setData({
          timer: setInterval(function () {
            let countdown_time = util.countdownTime(that.data.end_time)
            if (countdown_time.time < 1 || reps.data.activity_status === '2') {
              clearInterval(that.data.timer)
              that.setData({
                countdown: false
              })
            } else {
              that.setData({
                countdown_time: countdown_time
              })
            }

          }, 1000),
          apply_bargain_info: reps.data,
          end_time: reps.data.end_time
        })
      }
    })
  },
  // 我也要参加此活动
  signupActivityTap() {
    wx.redirectTo({
      url: '../bargain?id=' + this.data.activityId,
    })
  },
  bargainTap() {
    _bargain(this.data.activityId, { 'bargain_apply_id': this.data.bargain_apply_id }, reqs => {
      console.log(reqs)
      if (reqs.status) {
        this.setData({
          modalName: 'bargainSuccess',
          bargain_amount: reqs.data.bargain_amount
        })
        // 重新获取砍价记录列表
        this.get_apply_bargain_record()
        // 报名信息
        this.get_apply_bargain_info()
      }
    })
  },
  hideModal() {
    this.setData({
      modalName: ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    if (options.from === 'button') {
      return {
        path: '/pages/home/bargain/detail/detail?id=' + this.data.activityId + '&bargain_apply_id=' + this.data.bargain_apply_id
      }
    }
  }
})